#!/bin/bash

extensionList=('*.mov' '*.mp4' '*.avi' '*.mpg'  '*.qt' '*.mpeg' '*.mxf' '*.3gp' '*.mts' '*.wav' '*.aac' '*.m4a' '*.caf' '*.bwf' '*.aiff' '*.mp3' '*.gif' '*.png' '*.jpg' '*.jpeg' '*.tiff' '*.bmp' '*.psd' '*.raw' '*.tga')


TEMPFILE=$(mktemp)
EXTENSIONTEMP=$(mktemp)

for i in ${extensionList[@]}; do
	echo $i >> $EXTENSIONTEMP
done

#RETURNED=$(osascript -e 'set theChoices to {"Transfer to new Library","Ignore"}' -e 'set yourChoice to choose from list theChoices with prompt "Would you like to transfer Render Files with the Libraries or ignore them?"')

RETURNED=$(osascript -e 'set RENDERText to "Would you like to transfer Render Files with the Libraries or ignore them?"' -e 'set RENDERnote to "Note: Transferred Render files may not always relink to new libraries and may use additional space on the library share."' -e 'display alert RENDERText message RENDERnote as critical buttons {"Transfer","Ignore"} default button "Ignore"')

RETURN2=$(echo $RETURNED|awk -F '[\:]' '{print $NF}') #Makes this one word (Transfer or Ignore)

#echo $RETURN2

if [ $RETURN2 == "Ignore" ]; then
	echo "Skipping Render Files"
	echo '*/Render\ Files/*' >> $EXTENSIONTEMP
else
	echo "Copying Render Files"
	#cat $EXTENSIONTEMP
fi

echo "Please wait for AppleScript..."

originalFolder=$(osascript -e 'set thisFolder to (the POSIX path of (choose folder with prompt "Please select a directory with Final Cut Libraries"))')

mediaFolder=$(osascript -e 'set mediaFolder to (the POSIX path of (choose folder with prompt "Please select a folder for Media"))')

ERRLOG=$mediaFolder/$(date +%Y%m%d%H%M%S)errlog.log

libraryFolder=$(osascript -e 'set libraryFolder to (the POSIX path of (choose folder with prompt "Please select a folder for Libraries"))')

PROCEED=$(osascript -e 'set Proceed to "This will start the migration. Please leave the migration utility open until it is complete."' -e 'display dialog Proceed')

PROCEED2=$(echo $PROCEED |awk -F '[\:]' '{print $NF}')

if [[ $PROCEED2 == "OK" ]]; then
	:
else
	echo "USER HAS CANCELED"
	exit
fi

cd "$originalFolder"
ls -d *.fcpbundle >$TEMPFILE   #TEMPFILE ex. Untitled.fcpbundle \n smb.fcpbundle

while read i; do
#	mkdir "$libraryFolder/$i"
	rsync -av --progress --exclude-from $EXTENSIONTEMP "$originalFolder/$i" "$libraryFolder/"
	NOBUNDLE=$(basename "$i" .fcpbundle)_Media 														#strips .fcpbundle of off project to use as folder name, then adds _Media
	mkdir -p "$mediaFolder/$NOBUNDLE" 																#NFS/fcpproject (without bundle)
	#echo $i
	for e in ${extensionList[@]}; do
		cd "$originalFolder/$i"
		find . -type f -name $e |grep -v "/._"|while read m; do
			#echo $m
			dir=$(dirname "$m")																		#m is the file with it's path from the current directory. dir is the path from the project level to the file level.
			#echo $dir
			mkdir -p "$mediaFolder/$NOBUNDLE/$dir"													#This creates the directory for the .mov ex. MediaShare/Project_Media/event/original Media
			rsync -av --progress "$m" "$mediaFolder/$NOBUNDLE/$dir/"								#copies the file to the new directory
			echo "This may take a moment..."
			MD51=$(md5 "$m"|awk -F '[/=]' '{print $NF}') 
			NEWFILE=$(basename "$mediaFolder/$dir/$m")
			MD52=$(md5 "$mediaFolder/$NOBUNDLE/$dir/$NEWFILE"|awk -F '[/=]' '{print $NF}')			#compares MD5 of old file and new file
			if [[ $MD51 == $MD52 ]]; then
				echo $(basename "$m") "copied successfully."
				if [ ! -d "$libraryFolder/$i/$dir" ]; then											
					mkdir -p "$libraryFolder/$i/$dir"												#Creates file path down to the soft links directory
					ln -s "$mediaFolder/$NOBUNDLE/$dir/$NEWFILE" "$libraryFolder/$i/$dir/$NEWFILE"	#soft links new Media to New Library
				else
					ln -s "$mediaFolder/$NOBUNDLE/$dir/$NEWFILE" "$libraryFolder/$i/$dir/$NEWFILE"
				fi
			else
				echo "$(date +%Y%m%d%H%M%S) $m MD5 did not match" >> "$ERRLOG"
				echo $(basename "$m") "copy failed (This has been logged to $ERRLOG)."
			fi
		done
	done
done<$TEMPFILE

echo 'THE FCPX TRANSFER IS COMPLETE'
echo "End of Log" >> "$ERRLOG"
echo "Any problems found during the migration were reported to "$ERRLOG""
Quiet=$(osascript -e 'set FINISHED to "The migration has finished."' -e 'display dialog FINISHED buttons {"OK"} default button "OK"')
rm -f $TEMPFILE
rm -f $EXTENSIONTEMP








